﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Manager_ListTeacher : System.Web.UI.Page
{
    CommonClass ccobj = new CommonClass();
    DBClass dbobj = new DBClass();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bindgrid();
            binddepartment();
        } 
    }
    protected void bindgrid()
    {
        string strsql = "select * from Teacher ";
        if (Convert.ToInt32(Session["Isdetail"]) == 1)
            strsql += "where ClassID='" + Request.QueryString["stdid"].ToString() + "'";
        DataTable dstable = dbobj.GetDataSet(strsql, "tabteacher");
        GridView1.DataSource = dstable.DefaultView;
        GridView1.DataKeyNames = new string[] { "TeacherID" };
        GridView1.DataBind();
        Session["Isdetail"] = "";
    }
    protected void binddepartment()
    {
        string strdep = "select * from Department";
        DataTable dtddl = dbobj.GetDataSet(strdep, "tabdepartment");
        DropDownList1.DataSource = dtddl.DefaultView;
        DropDownList1.DataTextField = dtddl.Columns[1].ToString();
        DropDownList1.DataValueField = dtddl.Columns[0].ToString();
        DropDownList1.DataBind();
    }
    public string getClassName(string classid)
    {
        string strsql = "select ClassName from Class where ClassID='" + classid + "'";
        return dbobj.ExecScalar(strsql);
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "update")
        {
            Response.Redirect("../Manager/EditStudent.aspx?stdID=" + e.CommandArgument);
        }
        else
        {
            string strsql = "delete from Student where StudentID='" + e.CommandArgument + "'";
            dbobj.ExecNonQuery(strsql);
            Response.Write(ccobj.MessageBox("删除成功！"));
            bindgrid();
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (DropDownList1.SelectedIndex == -1 && (DropDownList2.SelectedIndex == -1 || DropDownList2.SelectedIndex == 0) && TextBox1.Text == "")
            Response.Write(ccobj.MessageBox("请选择系或班级，或输入查询关键字"));
        if (TextBox1.Text == "")
        {
            string strsql = "select * from Teacher where ClassID ";
            if (DropDownList2.SelectedIndex == 0)
            {
                strsql += "in( select * from Department where DepartmentID='" + DropDownList1.SelectedValue.ToString() + "')";
            }
            else
            {
                strsql += "='" + DropDownList2.SelectedValue.ToString() + "'";
            }
            DataTable dt = dbobj.GetDataSet(strsql, "tabteacher");
            GridView1.DataSource = dt.DefaultView;
            GridView1.DataKeyNames = new string[] { "TeacherID" };
            GridView1.DataBind();
        }
        else
        {
            string strsql = "select * from Teacher where ";
            if (DropDownList2.SelectedIndex == 0 || DropDownList2.SelectedIndex == -1)
            {
                strsql += "TeacherID like '%" + TextBox1.Text.Trim() + "%' or TeacherName like '%" + TextBox1.Text.Trim() + "%'";
            }
            else
            {
                strsql += "ClassID='" + DropDownList2.SelectedValue.ToString() + "' and TeacherID like '%" + TextBox1.Text.Trim() + "%' or TeacherName like '%" + TextBox1.Text.Trim() + "%'";
            }
            DataTable dt = dbobj.GetDataSet(strsql, "tabteacher");
            GridView1.DataSource = dt.DefaultView;
            GridView1.DataKeyNames = new string[] { "TeacherID" };
            GridView1.DataBind();
        }
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string strsql = "select * from Class where DepartmentID='" + DropDownList1.SelectedValue.ToString() + "'";
        DataTable dstable = dbobj.GetDataSet(strsql, "tabclass");
        DropDownList2.DataSource = dstable.DefaultView;
        DropDownList2.DataTextField = dstable.Columns[1].ToString();
        DropDownList2.DataValueField = dstable.Columns[0].ToString();
        DropDownList2.DataBind();
        DropDownList2.Items.Insert(0, "请选择");
    }
}